﻿#include "businessviewmodel.h"
#include "businessfile.h"
#include "businessnetwork.h"
#include <protocol/protocolConverter.h>
#include "businessgroup.h"
#include "businessuser.h"

#include <Debug/DEBUG.hpp>
#include <QString>
#include <QByteArray>
#include <QStringDecoder>

BusinessViewModel::BusinessViewModel() {}

void BusinessViewModel::updateUserList(nlohmann::json &users)
{
    // update the user view
    for (auto& i : users) {
        std::string account = i["Account"];
        if (i["Relation"] == "accepted") {
            emit userListAddItem(account.c_str());
        }
        else if (i["Relation"] == "pending") {
            emit requestListAddItem(account.c_str());
        }
    }
    emit initUserLoginMessage();
}

void BusinessViewModel::addMessageFromUser(const User &user, const std::string &message, int messageType, const std::string& time)
{
    std::string account = user["Account"];
    emit addMessage(user["Avatar"], account.c_str(), message.c_str(), time.c_str(), messageType);
}

void BusinessViewModel::addMessageFromSelf(const User &user, const std::string &message, int messageType, const std::string &time)
{
    std::string targetAccount = user["Account"];
    emit SIGaddMessageFromSelf(targetAccount.c_str(), message.c_str(), time.c_str(), messageType);
}



void BusinessViewModel::addMessageFromGroup(const User &user, uint64_t groupID, const std::string &message, int messageType, const std::string &time)
{
    std::string account = user["Account"];
    emit addGroupMessage(user["Avatar"], account.c_str(), groupID, message.c_str(), time.c_str(), messageType);
}

void BusinessViewModel::updateFileTransferInfo(const std::string &info)
{
    emit SignalUpdateFileTransferInfo(info.c_str());
}

void BusinessViewModel::updateFileList(nlohmann::json &files)
{
    BusinessFile& BFile = BusinessFile::getInstance();
    for (nlohmann::json& file : files) {
        std::string fileName = file["Name"];
        uint64_t fileID = file["FileID"];
        BFile.addFileNameAndID(fileName, fileID);
        emit addFileInfoToFileList(fileName.c_str());
    }
}

void BusinessViewModel::updateStatusbarText(std::shared_ptr<Protocol> protocol)
{
    ProtocolConverter converter;
    converter.setProtocolOption(BusinessNetWork::getInstance().getProtocolOption());
    converter.parseData((unsigned char*)protocol->data());

    std::string str;
    str.resize(converter.getParsedDataSize(), '\0');
    converter.copyParsedData((void*) str.data());

    QString text = convertToUTF16(str);

    emit signalUpdateStatusbarText(text);


}

void BusinessViewModel::updateGroupList(const nlohmann::json& groups)
{
    for (auto& i : groups) {
        std::string name = i["GroupName"];
        emit addGroupItem(name.c_str());
    }
}

void BusinessViewModel::updateGroupRequestList(const nlohmann::json &requests)
{
    emit clearGroupRequestList();
    for (auto& i : requests) {
        uint64_t groupID = i["GroupID"];
        std::string account = i["UserAccount"];
        User& user = BusinessUser::getInstance().getTargetUser(account);
        std::string groupName = BusinessGroup::getInstance().getTargetGroup(groupID)["GroupName"];

        emit addGroupRequestItem(user["Avatar"], account.c_str(), groupName.c_str());
    }
}

void BusinessViewModel::updateStatusbarPermanentText(const std::string &message, bool connected)
{
    emit SIGupdateStatusbarPermanentText(message.c_str(), connected);
}
QString BusinessViewModel::convertToUTF16(const std::string& input) {
    // 将原始数据从检测到的编码转换为UTF-16
    QStringDecoder decoder(QStringConverter::System);
    QByteArrayView view = input;
    QString decodedString = decoder(view);
    return decodedString;
}
